#!/usr/bin/env bash

# Usage: script/parse-examples [native|wasm]

# Exit immediately if a command exits with a non-zero status.
set -e

# Parse examples in 'native' or 'wasm' mode.
mode=${1:-native}

# Change directory to project root.
cd "$(dirname "$0")/.."

# Clone a GitHub repository to the examples directory
# clone_repo OWNER NAME SHA
function clone_repo {
  owner=$1
  name=$2
  sha=$3

  path=examples/$name
  if [ ! -d "$path" ]; then
    git clone "https://github.com/$owner/$name" "$path" --quiet
  fi

  pushd "$path" >/dev/null
  git fetch --quiet
  git reset --hard $sha --quiet
  popd >/dev/null
}

clone_repo joshvera effects 08f5f36f2600362685af593f4b327e933b60bf97
clone_repo PostgRest postgrest f80cfbf165f951a062b3cbedac4556019905ca49
clone_repo GaloisInc ivory 3d00324ad1c113c7e70957ff6a6d636d271d0fc4
clone_repo polysemy-research polysemy c37d485b614e98622f5e7473a478b781a6ad5c45
clone_repo github semantic b162132339622fe1e80e243f630fe092d5c0cbe1
clone_repo haskell haskell-language-server d397ef491ef1689d43028f4d3d01a42118292235
clone_repo AndrasKovacs flatparse ddae0996d2bdd0b5b092484dbe5829b4ee2ef1f6

for name in effects postgrest ivory polysemy semantic haskell-language-server flatparse; do
  script/parse-example "$name" "$mode"
done
